<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Recipes extends OC_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('recipes/recipe_model','rm');
		$this->load->model('home/home_model');
		$this->load->library('recaptcha');
		$this->load->model('events/events_model');
	}

	function _remap($method, $params = array())
	{
		parent::_remap($method, $params);

		if (method_exists($this, $method))
			return call_user_func_array(array($this, $method), $params);
		else
			show_404();
	}

	function init_header()
	{
		$this->header['meta'] = array(
			'description' => array('name' => 'description', 'content' => 'Haan Kreasi Ibu Juara'),
			'keywords' => array('name' => 'keywords', 'content' => 'Haan Kreasi Ibu Juara, recipes, kreasi ibu, kreatif, sajian  kreatif'),
		);
	}

	public function index()
	{
		$count = $this->rm->count(0,0,false);

		$this->load->library('Pagination');
		$config['total_rows'] = $count;
		$config['per_page'] = 12;
		$config['base_url'] = base_url().index_page().'/recipes/index/';

		/*style pagination*/
		$config['first_link	']			= '&lsaquo; First';
		$config['next_link']			= '&gt;&gt;';
		$config['prev_link']			= '&lt;&lt;';
		$config['last_link']			= 'Last &rsaquo;';
		//$config['uri_segment']			= 3;
		$config['full_tag_open']		= '<li>';
		$config['full_tag_close']		= '</li>';
		$config['first_tag_open']		= '<li>';
		$config['first_tag_close']		= '</li>';
		$config['last_tag_open']		= '<li>';
		$config['last_tag_close']		= '</li>';
		$config['cur_tag_open']			= '<li class="active"><span>';
		$config['cur_tag_close']		= '</span></li>';
		$config['next_tag_open']		= '<li>';
		$config['next_tag_close']		= '</li>';
		$config['prev_tag_open']		= '<li>';
		$config['prev_tag_close']		= '</li>';
		$config['num_tag_open']			= '<li>';
		$config['num_tag_close']		= '</li>';

		$this->pagination->initialize($config);
		$this->header['pagination'] = $this->pagination->create_links();

		$offset=$this->uri->segment(4);
		if(empty($offset)){$offset=0;}

		$this->header['items'] = $this->rm->get_items($config['per_page'], $offset,false);

		$this->header['widget_home']   		= $this->home_model->get_widget();
		$this->header['widget_footer']  	= $this->home_model->get_widget(2);
		$this->header['categories']    		= $this->rm->get_categories();
		$this->header['socmed_home']    	= $this->home_model->get_socmed();
		$this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['event_home']     = $this->home_model->get_event();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');

		$this->view('recipes/list_recipe_view');
	}

	public function categories()
	{
		$count = $this->rm->count(0,0,$this->uri->segment(3));

		$this->load->library('Pagination');
		$config['total_rows'] = $count;
		$config['per_page'] = 6;
		$config['base_url'] = base_url().index_page().'/recipes/categories/'.$this->uri->segment(3).'/';

		/*style pagination*/
		$config['first_link ']          = '&lsaquo; First';
		$config['next_link']            = '&gt;&gt;';
		$config['prev_link']            = '&lt;&lt;';
		$config['last_link']            = 'Last &rsaquo;';
		$config['uri_segment']          = 4;
		$config['full_tag_open']        = '<li>';
		$config['full_tag_close']       = '</li>';
		$config['first_tag_open']       = '<li>';
		$config['first_tag_close']      = '</li>';
		$config['last_tag_open']        = '<li>';
		$config['last_tag_close']       = '</li>';
		$config['cur_tag_open']         = '<li class="active"><span>';
		$config['cur_tag_close']        = '</span></li>';
		$config['next_tag_open']        = '<li>';
		$config['next_tag_close']       = '</li>';
		$config['prev_tag_open']        = '<li>';
		$config['prev_tag_close']       = '</li>';
		$config['num_tag_open']         = '<li>';
		$config['num_tag_close']        = '</li>';

		$this->pagination->initialize($config);
		$this->header['pagination'] = $this->pagination->create_links();

		$offset=$this->uri->segment(4);
		if(empty($offset)){$offset=0;}

		$this->header['items'] = $this->rm->get_items($config['per_page'], $offset,$this->uri->segment(3));

		$this->header['widget_home']    = $this->home_model->get_widget();
		$this->header['widget_footer']  = $this->home_model->get_widget(2);
		$this->header['categories']    	= $this->rm->get_categories();
		$this->header['socmed_home']    = $this->home_model->get_socmed();
		$this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['event_home']     = $this->home_model->get_event();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');

		$this->view('recipes/list_recipe_view');
	}


	public function detail($title=false,$id=false)
	{

		$this->header['meta'] = array(
			'description' => array('name' => 'description', 'content' => $this->rm->get_meta('descriptions',$id)),
			'keywords' => array('name' => 'keywords', 'content' => $this->rm->get_meta('keywords',$id)),
		);

		$this->header['source_bot']		= array(
			script_tag(site_url('recipes/themescript/rating.js'))
		);


		$this->header['detail'] = $this->rm->get_item_details($id);

		$this->header['widget_home']    = $this->home_model->get_widget();
		$this->header['widget_footer']  	= $this->home_model->get_widget(2);
		$this->header['categories']   	= $this->rm->get_categories();
		$this->header['comments']    	= $this->rm->get_comments($id);
		$this->header['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();
		$this->header['socmed_home']    = $this->home_model->get_socmed();
		$this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['event_home']     = $this->home_model->get_event();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');

		$this->view('recipes/detail_recipe_view');
	}

	public function result()
	{
		$this->header['items'] 			= $this->rm->get_result_recipes($this->input->post('find_serch'));
		$this->header['widget_home']    = $this->home_model->get_widget();
		$this->header['widget_footer']  = $this->home_model->get_widget(2);
		$this->header['categories']   	= $this->rm->get_categories();
		$this->header['socmed_home']    = $this->home_model->get_socmed();

		$this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
		$this->header['event_home']     = $this->home_model->get_event();        
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');

		$this->view('recipes/result_recipe_view');
	}

	public function do_comment()
    {
    	$this->recaptcha->recaptcha_check_answer();
        $title = $this->input->post('r_name');
        if($this->input->post('submit'))
        {
            unset($_POST['recaptcha_challenge_field']);
            unset($_POST['recaptcha_response_field']);
            unset($_POST['submit']);
            unset($_POST['r_name']);

            if($this->recaptcha->getIsValid()){
                $_POST['cr_created_date'] = date('Y-m-d H:i:s');
                $do_add = $this->rm->do_comment($_POST);
                if($do_add){
                    redirect('recipes/detail/'.slugify($title).'/'.$this->input->post('r_id'), 'refresh');
                }else{
                    redirect('recipes/detail/'.slugify($title).'/'.$this->input->post('r_id'), 'refresh');
                }
            }else{
                redirect('recipes/detail/'.slugify($title).'/'.$this->input->post('r_id'), 'refresh');
            }
        }
    }

    public function do_rating()
    {
    	$users_ip = $_SERVER['REMOTE_ADDR'];
    	$check_uip = $this->rm->check_user_ip($users_ip,$_POST['id']);
    	if($check_uip)
    	{
    		$params = array('r_id'=>$_POST['id'],'rating'=>1,'users_ip'=>$users_ip);
    		$do_rating = $this->rm->do_rating($params);
    		if($do_rating)
    			print('<span class="rating_message">Terimkasih...</div>');
    		else
    			print('<span class="rating_message">Anda Telah Melakukan Rating</div>');	
    	}else{
    		print('<span class="rating_message">Anda Telah Melakukan Rating</div>');
    	}
    }

}

/* End of file recipes.php */
/* Location: ./applications/module_front/recipes/controllers/recipes.php */